/** App更新 */
import request from "./request.js";
import config from './config.js';
import Base64 from "./Base64Utils.js";
import common from "./common.js";

function showModal(title, msg) {
	uni.showModal({
		title: title,
		content: msg
	})
}

function showToastNoIcon(title) {
	uni.showToast({
		icon:"none",
		title:title
	})
}

const checkLogin = function() {
	var user = uni.getStorageSync(config.CUR_CUSTOM);
	//console.log(user);
	if(!user) {
		//#ifdef MP-WEIXIN
		uni.showToast({
			title: '为获取优质服务,请先授权', 
			icon:"none",
			success() {
				//跳转到登陆页面
				uni.redirectTo({
					url: '/pages/login/wx'
				});
			}
		});
		//#endif
		
	} else {
		// user = JSON.parse(user); //转换成JSON对象
		uni.redirectTo({
			url: '/pages/index/index'
		});
	}
}

/**
 * 加密字符串
 */
function password(str) {
	const res = Base64.encodeBase64(encodeURI(str));
	return res;
}

function unPassword(str) {
	const res = Base64.decodeBase64(decodeURI(str));
	return res;
}

/**
 * 加密请求参数
 * @param {Object} params
 */
function processParams(params) {
	const paramsType = Object.prototype.toString.call(params);
	if(paramsType === '[object Object]') {
		params = JSON.stringify(params); //如果是对象则转换成字符串
	}
	
	params = encodeURI(params);
	params = Base64.encodeBase64(params);
	return params;
}

/** 检测用户是否授权 */
function checkAuthorize(scope) {
	return new Promise((resolve, reject) => {
		uni.authorize({
		  scope: "scope."+scope,
		  success: () => {
			resolve(); // 允许授权
		  },
		  fail: () => {
			reject(); // 拒绝授权
		  },
		});
	  });
}

/** 重新计算距离 */
function rebuildMeter(meter) {
	if(meter<=0) {
		return "距离未知"
	} else if(meter>0 && meter<1000) {
		return "距您 "+meter + " m";
	} else if(meter>=1000) {
		return "距您 " + (meter/1000).toFixed(1) + " km";
	}
}

function rad(d) {
	return d * Math.PI / 180.0; 
}

function calMeter(lat1, lng1, lat2, lng2) {
	if(!lat1 || !lng1 || !lat2 || !lng2) {return 0;}
	const radLat1 = rad(lat1);
	const radLat2 = rad(lat2);
	const a = radLat1 - radLat2;
	const b = rad(lng1) - rad(lng2);
	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; //输出为公里

	const distance = s;
	let distance_str = "";

	if (parseInt(distance) >= 1) {
		distance_str = distance.toFixed(1) + "km";
	} else {
		distance_str = distance * 1000 + "m";
	}
	// return rebuildMeter(parseInt(distance * 1000));
	return parseInt(distance * 1000);
}

function rebuildMoney(money) {
	if(!money) {money = 0;}
	return money.toFixed(2);
}

function buildIndexToTime(index) {
	let hour = 0;
	let min = 0;
	hour = parseInt(index/2);
	min = parseInt(index%2);
	let res = ((hour<10?"0":"")+hour)+":"+(min>0?"30":"00");
	return res;
}

function buildTimeToIndex(time) {
	const array = time.split(":");
	let hour = parseInt(array[0]);
	let min = parseInt(array[1]);
	return hour*2+(min>=30?1:0);
}

//生成开门时间段
function buildOpenDoorTime(order) {
	if(order.orderType==='2') {return "-";}
	let shop = order.shop ;
	if(!shop) {shop = {}; return "-";} //
	// console.log(shop, order)
	const preOpenDoor = shop.preOpenDoor?shop.preOpenDoor:15; //可提前开门的分钟数
	const suffixOpenDoor = shop.suffixOpenDoor?shop.suffixOpenDoor:5; //可推迟开门的分钟数
	
	return buildOpenDoorStartTime(order.startTime, preOpenDoor) + " - " +
			buildOpenDoorEndTime(order.endTime, suffixOpenDoor);
}
function buildOpenDoorStartTime(startTime, preMin) {
	// console.log("--->"+startTime, preMin)
	const array = startTime.split(":");
	let h = parseInt(array[0]);
	let m = parseInt(array[1]);
	m = m - Number(preMin);
	let hasPre = false;
	if(m<0) {
		m = 60 + m;
		hasPre = true;
	}
	if(hasPre) {
		h -= 1;
		if(h<0) {h = 24 + h;}
	}
	if(h<10) {h = "0"+h;}
	if(m<10) {m = "0"+m;}
	return h+":"+m;
}
function buildOpenDoorEndTime(endTime, suffixMin) {
	const array = endTime.split(":");
	let h = parseInt(array[0]);
	let m = parseInt(array[1]);
	m = m + Number(suffixMin);
	let hasPlus = false;
	if(m>=60) {
		m = m - 60;
		hasPlus = true;
	}
	if(hasPlus) {
		h += 1;
		if(h>=24) {
			h = h - 24;
		}
	}
	if(h<10) {h = "0"+h;}
	if(m<10) {m = "0"+m;}
	return h+":"+m;
}

export default {
	checkLogin: checkLogin,
	password: password,
	unPassword: unPassword,
	processParams: processParams,
	checkAuthorize: checkAuthorize,
	showModal: showModal,
	rebuildMeter: rebuildMeter,
	rebuildMoney: rebuildMoney,
	calMeter: calMeter,
	showToastNoIcon: showToastNoIcon,
	buildIndexToTime: buildIndexToTime,
	buildTimeToIndex: buildTimeToIndex,
	buildOpenDoorTime: buildOpenDoorTime,
};